package database;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.util.ArrayList;
import java.util.Arrays;

public class AirlineGui {
        private JFrame frame, passengerFrame, aircraftFrame;
        private JPanel panel, passengerPanel, aircraftPanel;
        private JButton passenger, aircraft, exit;
        private JButton reserve, checkin, delRes, back1;
        private JButton schedule, update, postReport, delFlight, back2;

        private String[] cities = { "New York", "Washington D.C.", "Baltimore",
                        "L.A.", "San Francisco", "Seattle", "Chicago", "Newark", "Detroit",
                        "Boston", "Raleigh", "Tucson", "Columbus", "Tampa", "Houston",
                        "Las Vegas", "Minneapolis", "Atlanta", "Pittsburgh", "Toronto" };

        private String[] months = { "Jan", "Feb", "Mar", "April", "May", "June",
                        "July", "Aug", "Sept", "Oct", "Nov", "Dec" };

        private Integer[] days = new Integer[31];
        private Integer[] years = new Integer[6];

        private static Database base;

        /**
         * @param args
         */
        public static void main(String[] args) {
            //base = new Database();
            //base.connectJDBC("br85570", "asdf");
            //System.out.println("Connected");
        	AirlineGui gui = new AirlineGui();

        	}

        public AirlineGui() {
                frame = new JFrame("Airline Database");
                frame.requestFocusInWindow();
                frame.setLocation(250, 250);
                frame.setSize(500, 500);
                frame.setResizable(false);
                frame.setAlwaysOnTop(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);

                panel = new JPanel();
                passenger = new JButton("Passenger Information");
                aircraft = new JButton("Aircraft Information");
                exit = new JButton("Exit");
                panel.add(passenger);
                panel.add(aircraft);
                panel.add(exit);
                frame.getContentPane().add(panel);

                passengerFrame = new JFrame("Update Passenger Information");
                passengerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                passengerFrame.setSize(500, 500);
                passengerFrame.setResizable(false);
                passengerFrame.setAlwaysOnTop(true);
                passengerFrame.setVisible(false);
                passengerPanel = new JPanel();
                reserve = new JButton("Reserve Flight");
                checkin = new JButton("Checkin Passenger");
                delRes = new JButton("Delete Reservation");
                back1 = new JButton("Previous Menu");
                passengerPanel.add(reserve);
                passengerPanel.add(checkin);
                passengerPanel.add(delRes);
                passengerPanel.add(back1);
                passengerFrame.getContentPane().add(passengerPanel);

                aircraftFrame = new JFrame("Update Aircraft Infromation");
                aircraftFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                aircraftFrame.setSize(500, 500);
                aircraftFrame.setResizable(false);
                aircraftFrame.setAlwaysOnTop(true);
                aircraftFrame.setVisible(false);
                aircraftPanel = new JPanel();
                schedule = new JButton("Schedule New Flight");
                update = new JButton("Update Flight");
                postReport = new JButton("Print Post Flight Report");
                delFlight = new JButton("Cancel Flight");
                back2 = new JButton("Previous Menu");
                aircraftPanel.add(schedule);
                aircraftPanel.add(update);
                aircraftPanel.add(postReport);
                aircraftPanel.add(delFlight);
                aircraftPanel.add(back2);
                aircraftFrame.getContentPane().add(aircraftPanel);

                passenger.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                passengerFrame.setLocation(frame.getLocation());
                                passengerFrame.setVisible(true);
                                frame.setVisible(false);
                        }
                });

                aircraft.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                aircraftFrame.setLocation(frame.getLocation());
                                aircraftFrame.setVisible(true);
                                frame.setVisible(false);
                        }
                });

                back1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                frame.setLocation(passengerFrame.getLocation());
                                passengerFrame.setVisible(false);
                                frame.setVisible(true);
                        }
                });

                back2.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                frame.setLocation(aircraftFrame.getLocation());
                                aircraftFrame.setVisible(false);
                                frame.setVisible(true);
                        }
                });

                exit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                System.exit(0);
                        }
                });

                reserve.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                reserve();
                        }
                });

                checkin.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                checkin();
                        }
                });

                schedule.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                schedule();
                        }
                });

                delRes.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                deleteReservation();
                        }
                });

                update.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                update();
                        }
                });

                postReport.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                postReport();
                        }
                });

                delFlight.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                deleteFlight();
                        }
                });
        }

        private void reserve() {
                for (int i = 0; i <= 30; i++) {
                        days[i] = new Integer(i + 1);
                }

                for (int i = 0; i <= 5; i++) {
                        years[i] = new Integer(i + 2013);
                }

                final JFrame reserveFrame = new JFrame("Passenger Reservation");
                reserveFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                reserveFrame.setSize(800, 800);
                reserveFrame.setResizable(false);
                reserveFrame.setAlwaysOnTop(true);
                reserveFrame.setVisible(true);
                reserveFrame.setLocation(passengerFrame.getLocation());
                passengerFrame.setVisible(false);

                NumberFormat ageFormat = NumberFormat.getIntegerInstance();
                ageFormat.setMaximumIntegerDigits(2);
                NumberFormat amountFormat = NumberFormat.getNumberInstance();
                amountFormat.setMaximumFractionDigits(2);
                NumberFormat ssnFormat = NumberFormat.getIntegerInstance();
                ssnFormat.setMaximumIntegerDigits(9);
                ssnFormat.setMinimumIntegerDigits(9);
                ssnFormat.setGroupingUsed(false);
                NumberFormat zipFormat = NumberFormat.getIntegerInstance();
                zipFormat.setMaximumIntegerDigits(5);
                zipFormat.setMinimumIntegerDigits(5);
                zipFormat.setGroupingUsed(false);
                NumberFormat phoneFormat = NumberFormat.getIntegerInstance();
                phoneFormat.setMaximumIntegerDigits(10);
                phoneFormat.setMinimumIntegerDigits(10);
                phoneFormat.setGroupingUsed(false);

                JPanel reservePanel = new JPanel();
                reservePanel.setLayout(new GridLayout(0, 2));
                JLabel ssnText = new JLabel("Social Security Number: ");
                final JFormattedTextField ssn = new JFormattedTextField(ssnFormat);
                JLabel fnameText = new JLabel("First Name: ");
                final JTextField fname = new JTextField();
                JLabel lnameText = new JLabel("Last Name: ");
                final JTextField lname = new JTextField();
                JLabel ageText = new JLabel("Age: ");
                final JFormattedTextField age = new JFormattedTextField(ageFormat);
                JLabel addressText = new JLabel("Address: ");
                final JTextField address = new JTextField();
                JLabel aptText = new JLabel("Apartment Number: ");
                final JTextField apt = new JTextField();
                JLabel cityText = new JLabel("City: ");
                final JTextField city = new JTextField();
                JLabel stateText = new JLabel("State: ");
                final JTextField state = new JTextField();
                JLabel zipText = new JLabel("Zip: ");
                final JFormattedTextField zip = new JFormattedTextField(zipFormat);
                JLabel teleHomeText = new JLabel("Home Telephone Number: ");
                final JFormattedTextField home = new JFormattedTextField(phoneFormat);
                JLabel teleOfficeText = new JLabel("Office Telephone Number: ");
                final JFormattedTextField office = new JFormattedTextField(phoneFormat);
                JLabel emailText = new JLabel("Email Address: ");
                final JTextField email = new JTextField();
                JLabel classText = new JLabel("Flight Class: ");
                String[] classType = { "First", "Business", "Economy" };
                final JComboBox flightclass = new JComboBox(classType);
                JLabel paidText = new JLabel("Amount Paid: ");
                final JFormattedTextField paid = new JFormattedTextField(amountFormat);
                JLabel flightText = new JLabel("Flight Number: ");
                final JComboBox flight = new JComboBox();
                JLabel srcText = new JLabel("Source City: ");
                final JComboBox source = new JComboBox(cities);
                JLabel dstText = new JLabel("Destination City: ");
                final JComboBox dst = new JComboBox(cities);
                JLabel dateText = new JLabel("Date: ");
                final JComboBox monthDate = new JComboBox(months);
                final JComboBox dayDate = new JComboBox(days);
                final JComboBox yearDate = new JComboBox(years);
                JPanel date = new JPanel();
                date.add(monthDate);
                date.add(dayDate);
                date.add(yearDate);

                JButton findFlight = new JButton("Find Flight");
                findFlight.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                // populate flight numbers
                                String sql = "select f1.flight_num, f1.source, f1.source_rank, f1.depart_TS, f2.destination, f2.dest_rank from FLIGHT f1, "
                                                + "(select flight_num, destination, dest_rank from FLIGHT where to_char(depart_TS, 'MMDDYYYY')='"
                                                + monthDate.getSelectedItem()
                                                + dayDate.getSelectedItem()
                                                + yearDate.getSelectedItem()
                                                + "' and destination='"
                                                + dst.getSelectedItem()
                                                + "') f2 where f1.source='"
                                                + source.getSelectedItem()
                                                + "' and to_char(f1.depart_TS, 'MMDDYYYY')='"
                                                + monthDate.getSelectedItem()
                                                + dayDate.getSelectedItem()
                                                + yearDate.getSelectedItem()
                                                + "' and "
                                                + "f1.flight_num=f2.flight_num and f1.source_rank < f2.dest_rank;";

                                String[] flightNums = base.populateDropDown(sql);

                                for (int i = 0; i < flightNums.length; i++) {
                                        flight.addItem(flightNums[i]);
                                        flight.showPopup();
                                }
                        }
                });

                JButton submit = new JButton("Submit");
                submit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                // See if there is a spot on the plane

                                // How does trigger work???????????????

                                String insert = "Insert into reservation(flight_num, depart_ts, flight_class, amount_paid, "
                                                + "o_phone, h_phone, street, apt_num, state, city, "
                                                + "zip, email, ssn, source_rank, dest_rank) Values ("
                                                + flight.getSelectedItem() + ", " + "(TimeStamp 'MMDDYYYY') = "
                                                + monthDate.getSelectedItem() + dayDate.getSelectedItem()
                                                + yearDate.getSelectedItem() + ", " + flightclass.getSelectedIndex() + ", "
                                                + paid.getText() + ", '" + home.getText() + "', '" + office.getText()
                                                + "', '" + address.getText() + "', '" + apt.getText() + "', '" + state.getText()
                                                + "', '" + city.getText() + "', " + zip.getText() + ", '" + email.getText()
                                                + "', " + ssn.getText() + ", " + source.getSelectedIndex() + ", "
                                                + dst.getSelectedIndex() + ");";
                                base.insertIntoDatabase(insert);

                                System.out.println(flightclass.getSelectedItem()); // test drop
                                // box
                                // selected--
                                // works as
                                // intended
                                // call method to send to database
                                frame.setLocation(reserveFrame.getLocation());
                                reserveFrame.dispose();
                                frame.setVisible(true);
                        }
                });
                JButton cancel = new JButton("cancel");
                cancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                passengerFrame.setLocation(reserveFrame.getLocation());
                                reserveFrame.dispose();
                                passengerFrame.setVisible(true);
                        }

                });

                reservePanel.add(ssnText);
                reservePanel.add(ssn);
                reservePanel.add(fnameText);
                reservePanel.add(fname);
                reservePanel.add(lnameText);
                reservePanel.add(lname);
                reservePanel.add(ageText);
                reservePanel.add(age);
                reservePanel.add(addressText);
                reservePanel.add(address);
                reservePanel.add(aptText);
                reservePanel.add(apt);
                reservePanel.add(cityText);
                reservePanel.add(city);
                reservePanel.add(stateText);
                reservePanel.add(state);
                reservePanel.add(zipText);
                reservePanel.add(zip);
                reservePanel.add(teleHomeText);
                reservePanel.add(home);
                reservePanel.add(teleOfficeText);
                reservePanel.add(office);
                reservePanel.add(emailText);
                reservePanel.add(email);
                reservePanel.add(classText);
                reservePanel.add(flightclass);
                reservePanel.add(paidText);
                reservePanel.add(paid);
                reservePanel.add(srcText);
                reservePanel.add(source);
                reservePanel.add(dstText);
                reservePanel.add(dst);
                reservePanel.add(dateText);
                reservePanel.add(date);
                reservePanel.add(new Panel());
                reservePanel.add(findFlight);
                reservePanel.add(flightText);
                reservePanel.add(flight);

                reservePanel.add(submit);
                reservePanel.add(cancel);

                reserveFrame.getContentPane().add(reservePanel);
        }

        private void checkin() {
                for (int i = 0; i <= 30; i++) {
                        days[i] = new Integer(i + 1);
                }

                for (int i = 0; i <= 5; i++) {
                        years[i] = new Integer(i + 2013);
                }

                NumberFormat ssnFormat = NumberFormat.getIntegerInstance();
                ssnFormat.setMaximumIntegerDigits(9);
                ssnFormat.setMinimumIntegerDigits(9);
                ssnFormat.setGroupingUsed(false);

                final JFrame checkinFrame = new JFrame("Passenger Checkin");
                checkinFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                checkinFrame.setSize(500, 500);
                checkinFrame.setResizable(false);
                checkinFrame.setAlwaysOnTop(true);
                checkinFrame.setVisible(true);
                checkinFrame.setLocation(passengerFrame.getLocation());
                passengerFrame.setVisible(false);

                JPanel checkinPanel = new JPanel();
                checkinPanel.setLayout(new GridLayout(0, 2));
                JLabel ssnText = new JLabel("Social Security Number: ");
                final JFormattedTextField ssn = new JFormattedTextField(ssnFormat);
                JLabel flightText = new JLabel("Flight Number: ");
                final JFormattedTextField flight = new JFormattedTextField(NumberFormat
                                .getIntegerInstance());
                JLabel dateText = new JLabel("Date: ");
                final JComboBox<String> monthDate = new JComboBox<String>(months);
                final JComboBox<Integer> dayDate = new JComboBox<Integer>(days);
                final JComboBox<Integer> yearDate = new JComboBox<Integer>(years);
                JPanel date = new JPanel();
                date.add(monthDate);
                date.add(dayDate);
                date.add(yearDate);
                JLabel baggageText = new JLabel("Baggage Carried: ");
                final JTextField baggage = new JTextField();
                JLabel seatNumText = new JLabel("Seat Number: ");
                final JFormattedTextField seatNum = new JFormattedTextField(
                                NumberFormat.getNumberInstance());
                JButton submit = new JButton("Submit");
                submit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                // call method to send to database
                                String checkin = "select check_in(" + ssn.getText() + ", "
                                                + flight.getText() + ", " + monthDate.getSelectedItem()
                                                + dayDate.getSelectedItem()
                                                + yearDate.getSelectedItem() + ", " + baggage.getText()
                                                + ", " + seatNum.getText() + ") from dual;";
                                base.insertIntoDatabase(checkin);
                                frame.setLocation(checkinFrame.getLocation());
                                checkinFrame.dispose();
                                frame.setVisible(true);
                        }
                });
                JButton cancel = new JButton("cancel");
                cancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                passengerFrame.setLocation(checkinFrame.getLocation());
                                checkinFrame.dispose();
                                passengerFrame.setVisible(true);
                        }

                });

                checkinPanel.add(ssnText);
                checkinPanel.add(ssn);
                checkinPanel.add(flightText);
                checkinPanel.add(flight);
                checkinPanel.add(dateText);
                checkinPanel.add(date);
                checkinPanel.add(baggageText);
                checkinPanel.add(baggage);
                checkinPanel.add(seatNumText);
                checkinPanel.add(seatNum);
                checkinPanel.add(submit);
                checkinPanel.add(cancel);

                checkinFrame.getContentPane().add(checkinPanel);
        }

        private void deleteReservation() {
                for (int i = 0; i <= 30; i++) {
                        days[i] = new Integer(i + 1);
                }

                for (int i = 0; i <= 5; i++) {
                        years[i] = new Integer(i + 2013);
                }

                NumberFormat ssnFormat = NumberFormat.getIntegerInstance();
                ssnFormat.setMaximumIntegerDigits(9);
                ssnFormat.setMinimumIntegerDigits(9);
                ssnFormat.setGroupingUsed(false);

                final JFrame delFrame = new JFrame("Cancel Reservation");
                delFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                delFrame.setSize(500, 500);
                delFrame.setResizable(false);
                delFrame.setAlwaysOnTop(true);
                delFrame.setVisible(true);
                delFrame.setLocation(passengerFrame.getLocation());
                passengerFrame.setVisible(false);

                JPanel delPanel = new JPanel();
                delPanel.setLayout(new GridLayout(0, 2));
                JLabel ssnText = new JLabel("Social Security Number: ");
                final JFormattedTextField ssn = new JFormattedTextField(ssnFormat);
                JLabel flightText = new JLabel("Flight Number: ");
                final JFormattedTextField flight = new JFormattedTextField(NumberFormat
                                .getIntegerInstance());
                JLabel dateText = new JLabel("Date: ");
                final JComboBox<String> monthDate = new JComboBox<String>(months);
                final JComboBox<Integer> dayDate = new JComboBox<Integer>(days);
                final JComboBox<Integer> yearDate = new JComboBox<Integer>(years);
                JPanel date = new JPanel();
                date.add(monthDate);
                date.add(dayDate);
                date.add(yearDate);

                JButton submit = new JButton("Submit");
                submit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                // call method to send to database
                                String delRes = "Select cancel_reservation( " + flight.getText() + ", " +
                                monthDate.getSelectedItem() + dayDate.getSelectedItem()
                                + yearDate.getSelectedItem() + ", " + ssn.getText() + ") from dual;";
                                base.executeQuery(delRes);
                                frame.setLocation(delFrame.getLocation());
                                delFrame.dispose();
                                frame.setVisible(true);
                        }
                });
                JButton cancel = new JButton("cancel");
                cancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                passengerFrame.setLocation(delFrame.getLocation());
                                delFrame.dispose();
                                passengerFrame.setVisible(true);
                        }

                });

                delPanel.add(ssnText);
                delPanel.add(ssn);
                delPanel.add(flightText);
                delPanel.add(flight);
                delPanel.add(dateText);
                delPanel.add(date);

                delPanel.add(submit);
                delPanel.add(cancel);

                delFrame.getContentPane().add(delPanel);
        }
        

        private void schedule() {
                final JFrame scheduleFrame = new JFrame("Schedule Flight");
                //scheduleFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                scheduleFrame.setSize(500, 500);
                scheduleFrame.setResizable(false);
                scheduleFrame.setAlwaysOnTop(true);
                scheduleFrame.setVisible(true);
                scheduleFrame.setLocation(aircraftFrame.getLocation());
                aircraftFrame.setVisible(false);

                final String[] cities = { "New York", "Washington D.C.", "Baltimore",
                                "L.A.", "San Francisco", "Seattle", "Chicago", "Newark",
                                "Detroit", "Boston", "Raleigh", "Tucson", "Columbus", "Tampa",
                                "Houston", "Las Vegas", "Minneapolis", "Atlanta", "Pittsburgh",
                                "Toronto" };
               

                String[] aircraftType = { "Boeing 737", "Boeing 747", "Boeing 767",
                                "Airbus 330", "Airbus 340", "Airbus 380" };

                for (int i = 0; i <= 30; i++) {
                        days[i] = new Integer(i + 1);
                }

                for (int i = 0; i <= 5; i++) {
                        years[i] = new Integer(i + 2013);
                }

                final NumberFormat timeFormat = NumberFormat.getIntegerInstance();
                timeFormat.setMaximumIntegerDigits(4);
                timeFormat.setMinimumIntegerDigits(4);
                timeFormat.setGroupingUsed(false);

                JPanel schedulePanel = new JPanel();
                schedulePanel.setLayout(new GridBagLayout());
                JLabel flightText = new JLabel("Flight Number: ");
                JFormattedTextField flight = new JFormattedTextField(NumberFormat
                                .getIntegerInstance());
                JLabel dateText = new JLabel("Date: ");

                final JComboBox monthDate = new JComboBox(months);
                final JComboBox dayDate = new JComboBox(days);
                final JComboBox yearDate = new JComboBox(years);
                JPanel date = new JPanel();
                date.add(monthDate);
                date.add(dayDate);
                date.add(yearDate);
                JLabel typeText = new JLabel("Aircraft Type: ");
                final JPanel sourcePanel = new JPanel();
                final JPanel destPanel = new JPanel();
                sourcePanel.setLayout(new GridLayout(0, 2));
                destPanel.setLayout(new GridLayout(0, 2));
                final JComboBox type = new JComboBox(aircraftType);
                JLabel sourceText = new JLabel("Source City: ");
                JLabel dstText = new JLabel("Destination City: ");
                JLabel dTimeText = new JLabel("Departure Time: ");
                final JFormattedTextField dtime = new JFormattedTextField(timeFormat);
                JLabel arriveText = new JLabel("Arrival Time: ");
                final JFormattedTextField arrive = new JFormattedTextField(timeFormat);
               
                final ComboBoxWrapper cityCombos = new ComboBoxWrapper();
                
                final PanelWrapper moreLegs = new PanelWrapper();
                
                
                JComboBox sourceCities = new JComboBox(cities);
                JComboBox destCities = new JComboBox(cities);
                
                sourceCities.setSelectedItem(null);
                destCities.setSelectedItem(null);

                
                
                final ActionListener cityListener = new ActionListener(){
        			public void actionPerformed(ActionEvent e) {
        			ComboBoxWrapper.remove((String)((JComboBox) e.getSource()).getSelectedItem());
        			}};
                
                sourceCities.addActionListener(cityListener);
                destCities.addActionListener(cityListener);
                int source_idx = cityCombos.addComboBox(sourceCities);
                int dest_idx = cityCombos.addComboBox(destCities);
                
                sourcePanel.add(sourceText);
                sourcePanel.add(dTimeText);
                sourcePanel.add(cityCombos.getComboBox(source_idx));
                sourcePanel.add(dtime);
                destPanel.add(dstText);
                destPanel.add(arriveText);
                destPanel.add(cityCombos.getComboBox(dest_idx));
                destPanel.add(arrive);
               
                JButton addCity = new JButton("Click to add destination");
                final JButton removeCity = new JButton("Click to remove destination");
                
                removeCity.addActionListener(new ActionListener() {
                	public void actionPerformed(ActionEvent e) {
                	if(moreLegs.getCount() == 1)
                	{
                		removeCity.setVisible(false);
                		removeCity.setEnabled(false);
                	}
                	cityCombos.removeComboBox();
                	moreLegs.removeLeg();
                	scheduleFrame.revalidate();
                	scheduleFrame.repaint();
                	}});
                
                addCity.addActionListener(new ActionListener() {
                	public void actionPerformed(ActionEvent e) {
                		JPanel nextLeg = new JPanel(new GridLayout(0, 3));
                		if(moreLegs.count == 0) {
                			nextLeg.add(new JLabel("Departure Time:"));
                			nextLeg.add(new JLabel("Destination"));
                			nextLeg.add(new JLabel("Arrival Time"));
                		}
                		else {
                			nextLeg.add(new JLabel(""));
                			nextLeg.add(new JLabel(""));
                			nextLeg.add(new JLabel(""));
                		}
                		nextLeg.add(new JFormattedTextField(timeFormat));
                		JComboBox city = new JComboBox(cities);
                		city.setSelectedItem(null);
                		city.addActionListener(cityListener);
                		int city_idx = cityCombos.addComboBox(city);
                		nextLeg.add(cityCombos.getComboBox(city_idx));
                		nextLeg.add(new JFormattedTextField(timeFormat));
                		moreLegs.add(nextLeg);
                		removeCity.setVisible(true);
                		removeCity.setEnabled(true);
                		scheduleFrame.revalidate();
                		scheduleFrame.repaint();
                	}
                });

                JButton submit = new JButton("Submit");
                submit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {

                                // call method to send to database
                                frame.setLocation(scheduleFrame.getLocation());
                                scheduleFrame.dispose();
                                frame.setVisible(true);
                        }
                });
                JButton cancel = new JButton("cancel");
                cancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                aircraftFrame.setLocation(scheduleFrame.getLocation());
                                scheduleFrame.dispose();
                                aircraftFrame.setVisible(true);
                        }
                });

                int row = 0;
                GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx = 0;
                c.gridy = row++;
                c.weightx = 0.5;
                c.weighty = 0.5;
                schedulePanel.add(flightText, c);
                c.gridx = GridBagConstraints.RELATIVE;
                schedulePanel.add(flight, c);
                c.gridx = 0;
                c.gridy = row++;
                schedulePanel.add(dateText, c);
                c.gridx = GridBagConstraints.RELATIVE;
                schedulePanel.add(date, c);
               
                c.gridx = 0;
                c.gridy = row++;
                schedulePanel.add(typeText, c);
                c.gridx = GridBagConstraints.RELATIVE;
                schedulePanel.add(type, c);
                c.gridx = 0;
                c.gridy = row++;
                c.gridwidth = 2;
                schedulePanel.add(sourcePanel, c);
                
                c.gridx = 0;
                c.gridy = row++;
                schedulePanel.add(destPanel, c);
                c.gridx = 0;
                c.gridy = row++;
                schedulePanel.add(moreLegs.getPanel(), c);
                
                c.anchor = GridBagConstraints.PAGE_END;
                c.gridx = 0;
                c.gridy = row++;
                c.gridwidth = 1;
                schedulePanel.add(addCity, c);
                c.gridx = GridBagConstraints.RELATIVE;
                removeCity.setVisible(false);
                removeCity.setEnabled(false);
                schedulePanel.add(removeCity, c);
                
                c.gridx = 0;
                c.gridy = row++;
                schedulePanel.add(submit, c);
                c.gridx = GridBagConstraints.RELATIVE;
                schedulePanel.add(cancel, c);
                scheduleFrame.getContentPane().add(schedulePanel);
                
                
        }

        private void update() {
                final JFrame updateFrame = new JFrame("Update Flight");
                updateFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                updateFrame.setSize(500, 500);
                updateFrame.setResizable(false);
                updateFrame.setAlwaysOnTop(true);
                updateFrame.setVisible(true);
                updateFrame.setLocation(aircraftFrame.getLocation());
                aircraftFrame.setVisible(false);

                String[] months = { "Jan", "Feb", "Mar", "April", "May", "June",
                                "July", "Aug", "Sept", "Oct", "Nov", "Dec" };

                NumberFormat timeFormat = NumberFormat.getIntegerInstance();
                timeFormat.setMaximumIntegerDigits(4);
                timeFormat.setMinimumIntegerDigits(4);
                timeFormat.setGroupingUsed(false);

                ButtonGroup radioGroup = new ButtonGroup();

                JPanel updatePanel = new JPanel();
                updatePanel.setLayout(new GridLayout(0, 2));
                JLabel flightText = new JLabel("Flight Number: ");
                final JFormattedTextField flight = new JFormattedTextField(NumberFormat
                                .getIntegerInstance());
                JLabel srcText = new JLabel("Source City: ");
                final JComboBox<String> source = new JComboBox<String>(cities);
                JLabel dstText = new JLabel("Destination City: ");
                final JComboBox<String> dst = new JComboBox<String>(cities);
                JLabel choose = new JLabel("Change Departure or Arrival Schedule");
                final JRadioButton depart = new JRadioButton("Departure");
                final JRadioButton arrive = new JRadioButton("Arrive");
                radioGroup.add(depart);
                radioGroup.add(arrive);
                JLabel spacer = new JLabel("");
                JLabel dateText = new JLabel("Date: ");
                final JComboBox<String> monthDate = new JComboBox<String>(months);
                final JComboBox<Integer> dayDate = new JComboBox<Integer>(days);
                final JComboBox<Integer> yearDate = new JComboBox<Integer>(years);
                JPanel date = new JPanel();
                date.add(monthDate);
                date.add(dayDate);
                date.add(yearDate);
                JLabel dTimeText = new JLabel("Time: ");
                final JFormattedTextField dtime = new JFormattedTextField(timeFormat);

                JButton submit = new JButton("Submit");
                submit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                // call method to send to database
                                        String update;
                                        if (depart.isSelected())
                                                update = "Update flight set depart_ts = (Timestamp 'mmddyyyy hh24:mi')="
                                                        + monthDate.getSelectedItem() + dayDate.getSelectedItem()
                                        + yearDate.getSelectedItem() +  dtime.getText() +
                                        " where flight_num = " + flight.getText() + " and destination = "
                                        + dst.getSelectedItem() + ";";
                                        else
                                                update = "Update flight set arrive_ts = (Timestamp 'mmddyyyy hh24:mi')="
                                                        + monthDate.getSelectedItem() + dayDate.getSelectedItem()
                                        + yearDate.getSelectedItem() +  dtime.getText() +
                                        " where flight_num = " + flight.getText() + " and source = "
                                        + source.getSelectedItem() + ";";
                                        base.executeUpdate(update);
                                frame.setLocation(updateFrame.getLocation());
                                updateFrame.dispose();
                                frame.setVisible(true);
                        }
                });
                JButton cancel = new JButton("cancel");
                cancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                aircraftFrame.setLocation(updateFrame.getLocation());
                                updateFrame.dispose();
                                aircraftFrame.setVisible(true);
                        }
                });

                updatePanel.add(flightText);
                updatePanel.add(flight);
                updatePanel.add(srcText);
                updatePanel.add(source);
                updatePanel.add(dstText);
                updatePanel.add(dst);
                updatePanel.add(choose);
                updatePanel.add(spacer);
                updatePanel.add(depart);
                updatePanel.add(arrive);
                updatePanel.add(dateText);
                updatePanel.add(date);
                updatePanel.add(dTimeText);
                updatePanel.add(dtime);

                updatePanel.add(submit);
                updatePanel.add(cancel);

                updateFrame.getContentPane().add(updatePanel);
        }

        private void postReport() {
                for (int i = 0; i <= 30; i++) {
                        days[i] = new Integer(i + 1);
                }

                for (int i = 0; i <= 5; i++) {
                        years[i] = new Integer(i + 2013);
                }

                final JFrame postFrame = new JFrame("Post Flight Report");
                postFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                postFrame.setSize(500, 500);
                postFrame.setResizable(false);
                postFrame.setAlwaysOnTop(true);
                postFrame.setVisible(true);
                postFrame.setLocation(aircraftFrame.getLocation());
                aircraftFrame.setVisible(false);

                JPanel postPanel = new JPanel();
                postPanel.setLayout(new GridLayout(0, 2));
                JLabel flightText = new JLabel("Flight Number: ");
                final JFormattedTextField flight = new JFormattedTextField(NumberFormat
                                .getIntegerInstance());
                JLabel dateText = new JLabel("Date: ");
                final JComboBox<String> monthDate = new JComboBox<String>(months);
                final JComboBox<Integer> dayDate = new JComboBox<Integer>(days);
                final JComboBox<Integer> yearDate = new JComboBox<Integer>(years);
                JPanel date = new JPanel();
                date.add(monthDate);
                date.add(dayDate);
                date.add(yearDate);
                JLabel dstText = new JLabel("Destination City: ");
                final JComboBox<String> dst = new JComboBox<String>(cities);

                JButton submit = new JButton("Submit");
                submit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                // call method to get report from database
                                        String onBoard = "Select countOnBoard(" + flight.getText()
                                                + ", " + monthDate.getSelectedItem() + dayDate.getSelectedItem()
                                    + yearDate.getSelectedItem() + ",0, " + dst.getSelectedIndex()
                                    + ") from dual;";
                                        String fDur = "Select flightDuration( " + flight.getText()
                                                + ", " + monthDate.getSelectedItem() + dayDate.getSelectedItem()
                                                + yearDate.getSelectedItem() + ", 0, " + dst.getSelectedIndex()+
                                                ") from dual;";
                                        int numbStops = dst.getSelectedIndex();
                                        int numBoard = 0;
                                        int duration = 0;
                                        try {
                                                                        numBoard = base.executeQuery(onBoard).getInt(1);
                                                                        duration = base.executeQuery(fDur).getInt(1);
                                                                } catch (SQLException e1) {
                                                                        // TODO Auto-generated catch block
                                                                        e1.printStackTrace();
                                                                }

                                final JFrame reportFrame = new JFrame(flight.getText()
                                                + " Post Report");
                                reportFrame
                                                .setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                                reportFrame.setSize(500, 500);
                                reportFrame.setResizable(false);
                                reportFrame.setAlwaysOnTop(true);
                                reportFrame.setLocation(postFrame.getLocation());
                                reportFrame.setVisible(true);

                                JPanel reportPanel = new JPanel();
                                reportPanel.setLayout(new BoxLayout(reportPanel,
                                                BoxLayout.Y_AXIS));
                                JLabel numPassengers = new JLabel("Number of Passengers: %d", numBoard);
                                JLabel numStops = new JLabel("Number of Stops: %d", numbStops);
                                JLabel time = new JLabel("Total Travel Time: %d", duration);
                                JButton ok = new JButton("OK");
                                ok.addActionListener(new ActionListener() {
                                        public void actionPerformed(ActionEvent e) {
                                                frame.setLocation(reportFrame.getLocation());
                                                reportFrame.dispose();
                                                frame.setVisible(true);
                                        }
                                });
                                reportPanel.add(numPassengers);
                                reportPanel.add(numStops);
                                reportPanel.add(time);
                                reportPanel.add(ok);

                                reportFrame.getContentPane().add(reportPanel);

                                postFrame.dispose();
                        }
                });
                JButton cancel = new JButton("cancel");
                cancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                aircraftFrame.setLocation(postFrame.getLocation());
                                postFrame.dispose();
                                aircraftFrame.setVisible(true);
                        }
                });

                postPanel.add(flightText);
                postPanel.add(flight);
                postPanel.add(dateText);
                postPanel.add(date);
                postPanel.add(dstText);
                postPanel.add(dst);

                postPanel.add(submit);
                postPanel.add(cancel);

                postFrame.getContentPane().add(postPanel);
        }

        private void deleteFlight() {
                final JFrame delFrame = new JFrame("Cancel Flight");
                delFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                delFrame.setSize(500, 500);
                delFrame.setResizable(false);
                delFrame.setAlwaysOnTop(true);
                delFrame.setVisible(true);
                delFrame.setLocation(aircraftFrame.getLocation());
                aircraftFrame.setVisible(false);

                String[] cities = { "New York", "Washington D.C.", "Baltimore", "L.A.",
                                "San Francisco", "Seattle", "Chicago", "Newark", "Detroit",
                                "Boston", "Raleigh", "Tucson", "Columbus", "Tampa", "Houston",
                                "Las Vegas", "Minneapolis", "Atlanta", "Pittsburgh", "Toronto" };

                for (int i = 0; i <= 30; i++) {
                        days[i] = new Integer(i + 1);
                }

                for (int i = 0; i <= 5; i++) {
                        years[i] = new Integer(i + 2013);
                }

                NumberFormat timeFormat = NumberFormat.getIntegerInstance();
                timeFormat.setMaximumIntegerDigits(4);
                timeFormat.setMinimumIntegerDigits(4);
                timeFormat.setGroupingUsed(false);

                JPanel delPanel = new JPanel();
                delPanel.setLayout(new GridLayout(0, 2));
                JLabel flightText = new JLabel("Flight Number: ");
                final JFormattedTextField flight = new JFormattedTextField(NumberFormat
                                .getIntegerInstance());
                JLabel dateText = new JLabel("Date: ");
                final JComboBox<String> monthDate = new JComboBox<String>(months);
                final JComboBox<Integer> dayDate = new JComboBox<Integer>(days);
                final JComboBox<Integer> yearDate = new JComboBox<Integer>(years);
                JPanel date = new JPanel();
                date.add(monthDate);
                date.add(dayDate);
                date.add(yearDate);
                JLabel sourceText = new JLabel("Source City: ");
                final JComboBox<String> source = new JComboBox<String>(cities);
                JLabel dstText = new JLabel("Destination City: ");
                final JComboBox<String> dst = new JComboBox<String>(cities);

                JButton submit = new JButton("Submit");
                submit.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                // call method to send to database
                                        String delete = "Delete * from flight, reservation where flight_num = "
                                                + flight.getText() + " and to_char(depart_ts 'MMDDYYYY') = " +
                                                monthDate.getSelectedItem() + dayDate.getSelectedItem()
                                    + yearDate.getSelectedItem() + ";";
                                        base.executeUpdate(delete);
                                frame.setLocation(delFrame.getLocation());
                                delFrame.dispose();
                                frame.setVisible(true);
                        }
                });
                JButton cancel = new JButton("cancel");
                cancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                aircraftFrame.setLocation(delFrame.getLocation());
                                delFrame.dispose();
                                aircraftFrame.setVisible(true);
                        }
                });

                delPanel.add(flightText);
                delPanel.add(flight);
                delPanel.add(dateText);
                delPanel.add(date);
                delPanel.add(sourceText);
                delPanel.add(source);
                delPanel.add(dstText);
                delPanel.add(dst);

                delPanel.add(submit);
                delPanel.add(cancel);

                delFrame.getContentPane().add(delPanel);
        }
        
        private static class ComboBoxWrapper {
        	private static ArrayList dropdowns;
        	private static ArrayList removedCities;
        	private static String[] cities = { "New York", "Washington D.C.", "Baltimore",
                     "L.A.", "San Francisco", "Seattle", "Chicago", "Newark", "Detroit",
                     "Boston", "Raleigh", "Tucson", "Columbus", "Tampa", "Houston",
                     "Las Vegas", "Minneapolis", "Atlanta", "Pittsburgh", "Toronto" };
        	public ComboBoxWrapper()
        	{
        		dropdowns = new ArrayList();
        		removedCities = new ArrayList();
        	}
        	
        	public int addComboBox(JComboBox jcb)
        	{
        		int size = dropdowns.size();
        		
        		for(int i = 0; i < removedCities.size(); i++)
        		{
        			jcb.removeItem(removedCities.get(i));
        			jcb.revalidate();
        			jcb.repaint();
        		}
        		dropdowns.add(jcb);
        		return size;
        	}
        	
        	public static void remove(String city)
        	{
        		removedCities = new ArrayList();
        		int dropdownIndex = -1;
        		int i = 0;
        		while(dropdownIndex < 0 && i < dropdowns.size())
        		{
        			String selItem = (String)((JComboBox)dropdowns.get(i)).getSelectedItem();
        			if(selItem != null && selItem.equals(city)) {
        				dropdownIndex = i;
        				removedCities.add(selItem);
        			}
        			else
        			{
        				if(selItem != null)
        				{
        					removedCities.add(selItem);
        				}
        			}
        			i++;
        		}
        		
        		if(dropdownIndex != -1){
        		for(i = dropdownIndex + 1; i < dropdowns.size(); i++)
        		{
        			JComboBox temp = ((JComboBox)dropdowns.get(i));
        			String selected = (String) temp.getSelectedItem();
        			temp.removeAllItems();
        			int index = -1;
    				for(int j = 0; j < cities.length; j++)
    				{
    					if(removedCities.indexOf(cities[j]) == -1)
    					{
    						temp.addItem(cities[j]);
    					}
    					else
    					{
    						if(selected != null && selected.equals(cities[j]))
    						{
    							selected = null;
    						}
    					}
    				}
    				
    				temp.setSelectedItem(selected);
    				temp.revalidate();
    				temp.repaint();
        		}
        		}
          		
        	}
        	
        	public static void removeComboBox()
        	{
        		dropdowns.remove(dropdowns.size() - 1);
        	}
        	
        	public JComboBox getComboBox(int index)
        	{
        		return (JComboBox)dropdowns.get(index);
        	}      
        }
        
        private static class PanelWrapper {
        		private JScrollPane scroll;
        		private JPanel panel;
        		private int count;
        		private ArrayList cities;
        		public PanelWrapper() { 
        			cities = new ArrayList();
        			panel = new JPanel(new GridLayout(0, 1));
        			scroll = new JScrollPane(panel);
        			scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        			scroll.setMaximumSize(new Dimension(500, 200));
        			scroll.setMinimumSize(new Dimension(500, 200));
        			
        			count = 0;
        			}
        		public void add(JPanel p) {
        			panel.add(p);
        			scroll.revalidate();
        			count++;
        		}
        		public JScrollPane getPanel() {
        			return scroll;
        		}
        		
        		public void removeLeg() {
        			panel.remove(panel.getComponentCount() - 1);
        		}
        		
        		public int getCount() {
        			return panel.getComponentCount();
        		}
        	}        	
        	
        }


